﻿var urlList = null;
var urlView = null;
var lastBGSave = new Date();

function GetPageFunction() {
    if (typeof (adFunctions) != 'undefined') {
        var functions = adFunctions.split(',');
        for (var index = 0; index < functions.length; index++) {
            if (typeof (eval(functions[index])) === 'function') {
                if (functions[index].match('^tbar[a-zA-Z]*\_')) {
                    BuildFunction(functions[index]);
                }
                else if (functions[index].match('^grd[a-zA-Z]*\_')) {
                    BuildGridAction(functions[index]);
                }
                else if (functions[index].match('\_')) {
                    BuildFieldAction(functions[index]);
                }
            }
        }
    }
    if (typeof (SetTextForControl) != 'undefined')
        SetTextForControl();
}
function BuildFunction(name) {
    var paths = name.split('_');
    var bt = Ext.getCmp(paths[0]);
    if (typeof (bt) != 'undefined') {
        eval(paths[0] + '.' + paths[1] + '=' + name);
    }
    else {
        bt = new Ext.Button({ id: paths[0], hidden: false, text: paths[0] });
        eval(paths[0] + '.' + paths[1] + '=' + name);
       
        // Insert separator before button action.
        var sep = new Ext.Toolbar.Separator({ id: "tbSep_" + name.split('_')[0].replace('tbar', '') });
        tbarMain.items.insert(1, sep);
        //tbarMain.items.get('tbSep_Release')
        
        // Insert button action.
        tbarMain.items.insert(2, bt);   
        tbarMain.syncSize();
    }
    ViewPort1.syncSize();
}
function BuildFieldAction(name) {
    var a = name.lastIndexOf('_');
    var id = name.substr(0, a);
    var fn = name.substr(a + 1);
    var bt = Ext.getCmp(id);
    if (typeof (bt) != 'undefined') {
        bt.addListener(fn, eval(name));
    }
}
function BuildGridAction(name) {
    var paths = name.split('_');
    var bt = Ext.getCmp(paths[0]);
    if (typeof (bt) != 'undefined') {
        bt.addListener(paths[1], eval(name));
    }
}
function LoadExeption(store, options, response, e) {
    Ext.Msg.alert('', e.message);
}

function LoadForm() {
    if (typeof (stoCard.data) == 'undefined')
        return;
    var record = stoCard.data.items[0];
    if (typeof (record) == 'undefined')
        return;
    var forms = Ext.ComponentMgr.all.filter('xtype', 'form')
    for (var ix = 0; ix < forms.items.length; ix++) {
        forms.items[ix].getForm().loadRecord(record);
    }
    if (typeof (LoadSpecialValuePGMaster298) != 'undefined') {
        setTimeout(function() { LoadSpecialValuePGMaster298() }, 3000);
    }
    if (typeof (LoadSpecialValueItem164) != 'undefined') {
        LoadSpecialValueItem164();
    }
    if (typeof (LoadSpecialValueLocation48) != 'undefined') {
        setTimeout(function() { LoadSpecialValueLocation48() }, 3000);
    }
    if (typeof (LoadSpecialValueRoute277card) != 'undefined') {
        LoadSpecialValueRoute277card();
    }
    NumberDisplayForCardPage(record);
}

function openFunctionCard() {
    var urlCard = 'List3.aspx?met=203&filter=Location_Code:{0}';
    if (stoCard.data.items[0].data.Code != null ) {
        var url = String.format(urlCard, stoCard.data.items[0].data.Code);
        load(url, stoCard.data.items[0].data.Code);
    } else {
        Ext.Msg.alert('', "Chưa có vị trí kho, vui lòng lưu trước");
     }
}

function openFunctionCardForItemUnitMeasure() {
    //var urlCard = 'List.aspx?edi=1&met=241&filter=Item_No:{0}';
//    var urlCard = 'List3.aspx?met=241&filter=Item_No:{0}';
//    var url = String.format(urlCard, stoCard.data.items[0].data.No);
//    load(url, stoCard.data.items[0].data.Code);
    
    var record = stoCard.data.items[0];
    if (record.get('No') != null) {
        var urlCard = 'List3.aspx?met=241&filter=Item_No:{0}';
        var url = String.format(urlCard, stoCard.data.items[0].data.No);
        load(url, stoCard.data.items[0].data.No);
        // url = '/Lookup.aspx?met=223&filter=Item_No:' + type;
    } else {
//        if (Ext.getCmp('No').getValue() != "") {
//            var urlCard = 'List3.aspx?met=241&filter=Item_No:{0}';
//            var url = String.format(urlCard, Ext.getCmp('No').getValue());
//            load(url, stoCard.data.items[0].data.Code);
//            // url = '/Lookup.aspx?met=223&filter=Item_No:' + Ext.getCmp('No').getValue();
//        }
//        else {
//            Ext.Msg.alert('', "Mã chưa điền");
    //        }
    Ext.Msg.alert('', "Chưa có thông tin mã mặt hàng");
    }

}

function openFunctionSalesItemName() {  
    var record = stoCard.data.items[0];
    if (record.get('No') != null) {
        var urlCard = 'List3.aspx?met=241&filter=Item_No:{0}';
        var url = String.format(urlCard, stoCard.data.items[0].data.No);
        load(url, stoCard.data.items[0].data.No);
        // url = '/Lookup.aspx?met=223&filter=Item_No:' + type;
    }
}

function loadList() {
    reloadFrameNode(this.name.replace('_IFrame', '').split("_")[0]);
    closeTabNode();
}

function loadListAfterSave() {
    reloadFrameNode(this.name.replace('_IFrame', '').split("_")[0]);  
}

function tbarNew_Click() {
    hdfMode.setValue('new');
}

function tbarEdit_Click() {
    hdfMode.setValue('edit');
}

function tbarDelete_Click() {
    if (hdfMode.getValue() != 'view') {
    }
}

function tbarCancel_Click() {
    var windowParent = window.parent.Ext.getCmp(name.replace('_IFrame', ''));
    windowParent.load(windowParent.autoLoad.url);
}

function tbarSave_Click_Before(item, type, action, extraParams) {
    if (typeof (stoCard.data) == 'undefined')
        return;
    var record = stoCard.data.items[0];
    if (typeof (record) == 'undefined')
        return;

//    if (tableName == "Item") {
//             var record = stoCard.data.items[0];
//            if (record.get('No') != null) {
//                if (Ext.getCmp('Base_Unit_of_Measure').getValue() == "") {
//                    Ext.Msg.alert('', "Đơn vị tính cơ bản không được trống");
//                    return false;
//                }
//               // url = '/Lookup.aspx?met=223&filter=Item_No:' + type;
//            }
//       

//    }
    var forms = Ext.ComponentMgr.all.filter('xtype', 'form');
    for (var ix = 0; ix < forms.items.length; ix++) {
        forms.items[ix].getForm().updateRecord(stoCard.data.items[0]);
    }
    DefaultFieldBeforeSave(stoCard.data.items[0]);
    stoCard.commitChanges();
    extraParams.OBJ = Ext.encode(stoCard.data.items[0].data);
}

function Lookup(el, trigger, index, e, url) {
    var wnd = Ext.getCmp('wndLookUp');
    ctrlLookup = el;
    if (wnd.autoLoad.url == url) {
        wnd.show(); return;
    }
    wnd.load(url);
    wnd.center();
    wnd.setTitle(TXTFind + el.fieldLabel.toLowerCase()); 
    wnd.show();
}

function NewLine() {
    mode = hdfMode.getValue();
    if (hdfMode.getValue() != 'view') {
        if (hdfMode.getValue() == 'view') return false;
        var NewRow = grdLines.store.addRecord(hdfItemDefault.getValue(), true);
        grdLines.startEditing(NewRow.index, 0);
    }
    return;
}

function DeleteLine() {
    if (!grdLines.hasSelection()) {
        Ext.Msg.alert('', TXTNolineSelect);
        return;
    }
    Ext.Msg.confirm(TXTConfirm, TXTConfirmDelete, function(btn) {
        if (btn == 'yes') {
            if (DeleteLines(btnDeleteLines, "", "click", null, grdLines)) {
                hdFunctions.setValue('DeleteLine');
                doSaveBG();
            }
            if (Ext.getCmp('btnDeleteLines'))
                Ext.getCmp('btnDeleteLines').enable();
        }
    });
}

function doSaveBG() {
    var now = new Date();
    if (now.getTime() - lastBGSave.getTime() > 200)
        tbarSaveBG.fireEvent('Click');
    lastBGSave = now;
}

function reloadFrame() {   
    var windowParent = window.parent.Ext.getCmp(this.name.replace('_IFrame', ''));
    windowParent.load(this.location);
}

function reloadFrame2(url) {
    if (tableName == "Item") {
        var windowParent = window.parent.Ext.getCmp(this.name.replace('_IFrame', ''));
        var record = stoCard.data.items[0];
        //  windowParent.title = windowParent.title + stoCard.data.items[0].data.No;
        // windowParent.id = windowParent.id + stoCard.data.items[0].data.No;
        //  windowParent.load(url);
        //  window.parent.load(url, windowParent.title + "-" + stoCard.data.items[0].data.No, windowParent.id + "_" + stoCard.data.items[0].data.No);
        // load(url, stoCard.data.items[0].data.No)
        var tab = window.parent.panelMain.getActiveTab();
        window.parent.panelMain.closeTab(tab, "hide");
        window.parent.load(url, windowParent.title.split('-')[0] + "-" + stoCard.data.items[0].data.No, windowParent.id.split('_')[0] + "_" + stoCard.data.items[0].data.No);
        //  windowParent.hide();
    } else {
    var windowParent = window.parent.Ext.getCmp(this.name.replace('_IFrame', ''));
    windowParent.load(url);
    }
    if ( tableName == "Location") {
        var windowParent = window.parent.Ext.getCmp(this.name.replace('_IFrame', ''));
        var record = stoCard.data.items[0];
        //  windowParent.title = windowParent.title + stoCard.data.items[0].data.No;
        // windowParent.id = windowParent.id + stoCard.data.items[0].data.No;
        //  windowParent.load(url);
        //  window.parent.load(url, windowParent.title + "-" + stoCard.data.items[0].data.No, windowParent.id + "_" + stoCard.data.items[0].data.No);
        // load(url, stoCard.data.items[0].data.No)
        var tab = window.parent.panelMain.getActiveTab();
        window.parent.panelMain.closeTab(tab, "hide");
        window.parent.load(url, windowParent.title.split('-')[0] + "-" + stoCard.data.items[0].data.Code, windowParent.id.split('_')[0] + "_" + stoCard.data.items[0].data.Code);
        //  windowParent.hide();
    } else {
        var windowParent = window.parent.Ext.getCmp(this.name.replace('_IFrame', ''));
        windowParent.load(url);
    }
}

var serviceUrl = '';
var ShowLink = function() {
    this.body.dom.innerHTML = serviceUrl;
}
